package main

import (
	"fmt"
	"sort"
)

func sortMap (data map[int]string){
	var slice []int
	for k, _ := range data{
		slice = append(slice,k)
	}
	sort.Ints(slice)
	for _,k := range slice{
		fmt.Printf("key=%v,value=%v\n",k,data[k])
	}
}

func main(){
	//使用map必须用make进行空间分配
	//map集合无序
	var a map[string]string = make(map[string]string, 5)
	a["no1"] = "张三"
	a["no2"] = "李四"
	fmt.Println(a)

	//map的crud
	delete(a,"no1")	//删除元素
	fmt.Println(a)
	//删除所有元素1.遍历删除2.重新分配空间
	//查找元素
	val, ok := a["no1"]
	if ok {
		fmt.Printf("有no1 key 值为%v\n",val)
	} else {
		fmt.Printf("没有no1 key\n")
	}

	//map的遍历	只能使用for range遍历
	for key,value := range a{
		fmt.Printf("key =%v,value =%v\n",key,value)
	}

	//map切片
	var maps []map[string]string = make([]map[string]string, 3)	//初始化切片
	maps[0] = make(map[string]string,2)	//初始化MAP
	maps[0]["name"] = "张三"
	maps[0]["age"] = "19"
	for index,value := range maps{
		fmt.Printf("index =%v,value =%v\n",index,value)
	}

	//map排序
	//先将map的key存在一个int的切片中
	//将切片进行排序
	//从遍历切片获取元素
	//自动扩容
	var data map[int]string = make(map[int]string,5)
	data[1] = "张三"
	data[2] = "李四"
	data[3] = "王五"
	data[4] = "赵六"
	for k,v := range data{
		fmt.Printf("key=%v,value=%v\n",k,v)
	}
	fmt.Println("------------------------------")
	sortMap(data)
}